
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: '',
    activeKey: 0,
    sidebar: [],
    cates: [],
    list: [],

    categoryState: {}
  },

  onChange(event) {
    let index = event.detail;
    let categoryId = this.data.sidebar[index].id;


    this.saveCurrentCategoryState();

    wx.request({
      url: 'http://admin.bsmall.byesame.com/wx/catalog/current',
      data: {
        id: categoryId
      },
      method: "GET",
      success: (res) => {
        if (res.statusCode == '200') {
          this.setData({
            activeKey: index,
            cates: res.data.data.currentCategory,
            list: res.data.data.currentSubCategory
          });


          this.restoreCategoryState(categoryId);
        }
      },
      fail: function () {

      },
      complete: function () {

      }
    })
  },


  saveCurrentCategoryState() {
    const {
      activeKey,
      sidebar,
      categoryState
    } = this.data;
    if (sidebar.length > 0 && activeKey >= 0) {
      const currentCategoryId = sidebar[activeKey].id;
      categoryState[currentCategoryId] = {
        activeKey: activeKey,
        cates: this.data.cates,
        list: this.data.list,
        scrollTop: 0
      };
      this.setData({
        categoryState
      });
    }
  },


  restoreCategoryState(categoryId) {
    const {
      categoryState
    } = this.data;
    if (categoryState[categoryId]) {
      const savedState = categoryState[categoryId];
      this.setData({
        activeKey: savedState.activeKey,
        cates: savedState.cates,
        list: savedState.list
      });
    }
  },

  getList() {
    wx.request({
      url: 'http://admin.bsmall.byesame.com/wx/catalog/index',
      data: {},
      method: "GET",
      success: (res) => {
        if (res.statusCode == '200') {
          this.setData({
            sidebar: res.data.data.categoryList,
            cates: res.data.data.currentCategory,
            list: res.data.data.currentSubCategory
          });
          console.log(this.data.list);


          if (res.data.data.categoryList.length > 0) {
            const firstCategoryId = res.data.data.categoryList[0].id;
            this.data.categoryState[firstCategoryId] = {
              activeKey: 0,
              cates: res.data.data.currentCategory,
              list: res.data.data.currentSubCategory
            };
          }
        }
      },
      fail: function () {

      },
      complete: function () {

      }
    })
  },
  goSearch() {
    wx.navigateTo({
      url: '/pages/search/search'
    });
  },
  onImageTap(event) {
    const {
      item,
      index
    } = event.currentTarget.dataset;
    // console.log('点击了商品:', item, index);


    wx.navigateTo({
      url: `/pages/product/detail?id=${item.id}&name=${item.name}`
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

    const {
      activeKey,
      sidebar,
      categoryState
    } = this.data;
    if (sidebar.length > 0 && activeKey >= 0) {
      const currentCategoryId = sidebar[activeKey].id;
      this.restoreCategoryState(currentCategoryId);
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    // 页面隐藏时保存当前状态
    this.saveCurrentCategoryState();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    // 页面卸载时保存当前状态
    this.saveCurrentCategoryState();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})